#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<string.h>
#include<errno.h>
int main()
{
  pid_t id = fork();
  if(id == 0)
  {
    //子进程
    int count = 5;
    while(count--)
    {
      printf("我是一个子进程，我的pid是:%d,父进程ppid是:%d\n",getpid(),getppid());
      sleep(1);
    }
    //int a = 100;
    // a /= 0;
     exit(1);
  }
  
  //sleep(10);
  //父进程
  // int rid = wait(NULL);
  int status = 0;
  int rid = waitpid(id,&status,0);
  if(rid > 0)
  {
    //等待成功
    //printf("wait success pid->%d, exit code:%d,exit signal:%d\n",rid,(status>>8)&0xFF,status&0x7F);
    if(WIFEXITED(status))
      printf("wait success pid->%d, exit code:%d\n",rid,WEXITSTATUS(status));
    else
      printf("程序异常！！\n");
  }
  else
  {
    //等待失败
    printf("wait failed %d -> %s",errno,strerror(errno));
  }
  //sleep(10);
  return 0;
}
